<?php
//http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/
?>
<form action="<?php echo $this->baseUrl(); ?>/tarefa/fila" method="get" id="form-pesquisa">
    Usuário: <?php echo $this->formSelect('fk_usuario', $this->fk_usuario, array('id' => 'fk_usuario'), $this->aUsuarios); ?>
    <input type="submit" value="Pesquisar">
</form>

<h3>Tarefas não finalizadas (<?php echo count($this->rowSetTarefas); ?>)</h3>
<form action="<?php echo $this->baseUrl(); ?>/tarefa/fila/gravar" method="post">
    <table id="table-1" cellspacing="0" cellpadding="2">
        <tr>
            <td>O.EV</td>
            <td>O.U.</td>
            <td>Projeto</td>
            <td>Tarefa</td>
        </tr>
    <?php
    $dateAtual = new DateTime(date('Y-m-d'));
    foreach ($this->rowSetTarefas as $rowTarefa) :
        $dtPrevisaoFim = new DateTime($rowTarefa->dt_previsao_fim);
        $class = 'ui-state-highlight';
        // Atrasada
        if ($dtPrevisaoFim < $dateAtual) {
            $class = 'ui-state-error';
        } elseif ($dateAtual == $dtPrevisaoFim) {
            $class= 'ui-state-default';
        }
    ?>
        <tr class="<?php echo $class; ?>" id="<?php echo $rowTarefa->id_tarefa; ?>">
            <td><?php echo $rowTarefa->nr_fila_organizacao; ?></td>
            <td align="center">
                <input type="hidden" name="ordem[<?php echo $rowTarefa->id_tarefa; ?>]" value="<?php echo $rowTarefa->nr_fila_usuario; ?>" id="input-ordem-<?php echo $rowTarefa->id_tarefa; ?>">
                <span id="span-ordem-<?php echo $rowTarefa->id_tarefa; ?>"><?php echo $rowTarefa->nr_fila_usuario; ?></span>
            </td>
            <td><?php echo $this->aProjetos[$rowTarefa->fk_projeto]; ?></td>
            <td><?php echo '[' . $rowTarefa->id_tarefa . '] ' . $rowTarefa->st_nome; ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <input type="submit" value="Gravar">
</form>
<script type="text/javascript" src="<?php echo $this->baseUrl(); ?>/js/jquery/jquery.tablednd_0_5.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    // Initialise the table
    $("#table-1 tr:even').addClass('alt')");

    $("#table-1").tableDnD({
        onDragClass: "even",
        onDrop: function(table, row) {
            //var indice = $(row).index() + 1;
            var rows = table.tBodies[0].rows;

            for (var i=1; i<rows.length; i++) {
                //$(rows[i]).children().eq(0).html(i);
                $("#input-ordem-" + rows[i].id).val(i);
                $("#span-ordem-" + rows[i].id).html(i);
            }
        }
    });
    $("#fk_usuario").change(function() { $("#form-pesquisa").submit() });
});
</script>